//
//  RankViewController.m
//  Cartoon
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "RankViewController.h"
#import "Rank.h"
#import "RankTableViewCell.h"
#import "RankTabelViewHeaderFooterView.h"
#import "CartoonDetailsViewController.h"
#import "RankDetailViewController.h"
@interface RankViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *arr;

@end

@implementation RankViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"排行";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SelfViewWidth, SelfViewHeight -64-49) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[RankTableViewCell class] forCellReuseIdentifier:@"Rank_cell"];
    [self handleDataTableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return self.arr.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    Rank *rank = [self.arr objectAtIndex:section];
    return rank.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    Rank *rank = [self.arr objectAtIndex:indexPath.section];
    NSArray *arr = rank.data;
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    Rank *rankNew = [Rank modelWithDic:dic];
    RankTableViewCell *cell = [RankTableViewCell rankCellWithTableView:tableView];
    cell.rankModel = rankNew;
    return cell;
    

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 110;
}

- (void)handleDataTableView
{
    [NetHandler dataWithUrl:@"http://mobilev3.ac.qq.com/Rank/rankListForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/0DB20220-FBA8-4267-AA65-5DC78AFBCEB4" complant:^(id data) {
        self.arr = [NSMutableArray array];
        NSArray *dataArr = [data objectForKey:@"data"];
        self.arr = [Rank modelsWithArr:dataArr];
        [self.tableView reloadData];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    static NSString *str = @"headerReuse";
    RankTabelViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:str];
    if (!view) {
        view = [[RankTabelViewHeaderFooterView alloc] initWithReuseIdentifier:str];
    }
    
    Rank *rank = [self.arr objectAtIndex:section];
    view.titleLabel.text = rank.title;
    [view.rank_icon_urlImageView sd_setImageWithURL:[NSURL URLWithString:rank.rank_icon_url]placeholderImage:[UIImage imageNamed:@"5"]];
    // 可以使用tag, 方便的在按钮点击方法中, 获取点击到的section.
    view.button.tag = section + 1000;
    [view.button setTitle:@"获取更多 >" forState:UIControlStateNormal];
    [view.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return view;

 }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CartoonDetailsViewController *carDetail = [[CartoonDetailsViewController alloc] init];
    
    Rank *rank = [self.arr objectAtIndex:indexPath.section];
    NSArray *arr = rank.data;
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    
    Rank *rankNew = [Rank modelWithDic:dic];
    
    
    
    carDetail.idtext = rankNew.comic_id;
    
    [self.navigationController pushViewController:carDetail animated:YES];
}
- (void)buttonAction:(UIButton *)btn
{
    RankDetailViewController *vc = [[RankDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    Rank *rank = [self.arr objectAtIndex:btn.tag - 1000];
    vc.rank_id = [rank.rank_id stringValue];
    // 获取tag, 就获取到了section
//    NSLog(@"%ld", btn.tag);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
